package com.freshdirect.src.controller;

import com.freshdirect.src.model.Product;
import com.freshdirect.src.model.ShoppingCart;
import com.freshdirect.src.service.ProductService;
import com.freshdirect.src.service.ShoppingCartService;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ProductController {

        @RequestMapping(value="/product/{id}", method = RequestMethod.GET)
         public String showProduct(@PathVariable int id, Model model,HttpSession session) {
            ProductService productService= new ProductService();
            
            Product product = productService.getProductbyId(id);
            
            if(product != null){
                List<Product> suggestions = productService.getSuggestions(id);
                model.addAttribute("product", product);
                model.addAttribute("cart",session.getAttribute("cart"));
                model.addAttribute("suggestions", suggestions);
                return "productpage";
            }
            else{
                model.addAttribute("cart",session.getAttribute("cart"));
                model.addAttribute("messagebad","There is not such a product!");
                return "redirect:/";
            }
        }
        
        @RequestMapping(value={"/search"}, method = RequestMethod.GET)
        public String searchForm(@RequestParam Map<String,String> params,Model model) {
               if(params != null){
                ProductService productService = new ProductService();
                List<Product> products = productService.search(params);
                model.addAttribute("products",products);
                if(params.get("categories")!=null && !params.get("categories").equals("")){
                    model.addAttribute("categories",params.get("categories"));
                }
                if(params.get("suppliers")!=null && !params.get("suppliers").equals("")){
                    model.addAttribute("suppliers",params.get("suppliers"));
                }
                if(params.get("keywords")!=null && !params.get("keywords").equals("")){
                    model.addAttribute("keywords",params.get("keywords"));
                }
               }
                return "search";
        }
        
        @RequestMapping(value="/product/category/{id}", method = RequestMethod.GET)
         public String showCategory(@PathVariable String id, Model model) {
                model.addAttribute("category", "FRUITS");
                ProductService productService= new ProductService();
                List<Product> products = productService.getProductsbyGroup(id);
                model.addAttribute("products", products);
                model.addAttribute("id", id.toUpperCase() );
                return "category";
        }
        
        @RequestMapping(value="/product/addtocart", method = RequestMethod.GET)
         public String addToCart(@RequestParam("id") int id,@RequestParam("n") int n, Model model,HttpSession session,HttpServletRequest request) {
                if(session.getAttribute("cart")==null){
                    System.out.println("New Shopping Cart");
                    ShoppingCart cart = new ShoppingCart();
                    ShoppingCartService scs= new ShoppingCartService();
                    scs.addProduct(id,n,cart);
                    session.setAttribute("cart", cart);
                }
                else{
                    System.out.println("There is Shopping Cart");
                    ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                    ShoppingCartService scs= new ShoppingCartService();
                    scs.addProduct(id,n,cart);
                    session.setAttribute("cart", cart);
                }
                
                
                model.addAttribute("cart",session.getAttribute("cart"));
                model.addAttribute("message", "Product added succesfully in the Shopping Cart");
                String referer = request.getHeader("Referer");
                return "redirect:/user/mycart";
                //return "redirect:/product/" + id;
        }

       @RequestMapping(value="/product/removefromcart", method = RequestMethod.GET)
         public String removeFromCart(@RequestParam("id") int id, Model model,HttpSession session) {
                if(session.getAttribute("cart")==null){
                    model.addAttribute("message", "There isn't such a product in the Shopping Cart");
                }
                else{
                    System.out.println("There is Shopping Cart");
                    ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                    ShoppingCartService scs= new ShoppingCartService();
                    scs.removeProduct(id,cart);
                    session.setAttribute("cart", cart);
                    model.addAttribute("message", "Product removed succesfully from the Shopping Cart");
                }
                model.addAttribute("cart",session.getAttribute("cart"));
                return "redirect:/user/mycart";
        }
        
        @RequestMapping(value="/product/updatetocart", method = RequestMethod.GET)
         public String updateToCart(@RequestParam("id") int id, @RequestParam("n") int n, Model model,HttpSession session) {
                if(session.getAttribute("cart")==null){
                    model.addAttribute("message", "There isn't such a product in the Shopping Cart");
                }
                else{
                    System.out.println("There is Shopping Cart");
                    ShoppingCart cart = (ShoppingCart)session.getAttribute("cart");
                    ShoppingCartService scs= new ShoppingCartService();
                    scs.updateProduct(id,n,cart);
                    session.setAttribute("cart", cart);
                    model.addAttribute("message", "Product updated succesfully to the Shopping Cart");
                }
                model.addAttribute("cart",session.getAttribute("cart"));
                return "redirect:/user/mycart";
        }
         
        @RequestMapping(value="/product/stockup", method = RequestMethod.GET)
         public String stockUp(@RequestParam("id") int id, @RequestParam("n") int n, Model model,HttpSession session) {
                
             if(session.getAttribute("adminName")!=null){
                ProductService scs= new ProductService();
                scs.updateQuantity(id,n);                
                return "redirect:/admin/newproduct";
             }
             else{
                 return "redirect:/";
             }
        }

}
